<?php

namespace App\Http\Middleware;

use App\Models\Permission;
use Closure;

class AdminAuth
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
       $permissions = $request->route()->getAction('permissions');

        if($permissions){

            $my_permissions = \Auth::guard('admin')->user()->permissions();

            foreach ($permissions as $permission){
                if(!in_array($permission,$my_permissions)){
                    return back();
                }
            }
        }

       return $next($request);
    }
}
